package com.sg.service.base.app.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.base.app.req.QueryEvaTepCyclePlanDetailReq;
import com.sg.dto.base.app.req.QueryEvaTepCyclePlanListReq;
import com.sg.entity.OmsTspeTimerImplementEvaCyclePlan;
import com.sg.service.base.app.MOmsTspeTimerImplementEvaCyclePlanService;
import com.wicket.okrapp.common.dto.FieldInfo;
import com.wicket.okrapp.common.exception.BizException;
import com.wicket.okrapp.dal.mapper.mbg.OmsTspeTimerImplementEvaCyclePlanMapper;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author ：
 * @version 1.0
 * @since 2022/1/19 15:39
 */
@Service
public class MOmsTspeTimerImplementEvaCyclePlanServiceImpl
        extends ServiceImpl<OmsTspeTimerImplementEvaCyclePlanMapper, OmsTspeTimerImplementEvaCyclePlan>
        implements MOmsTspeTimerImplementEvaCyclePlanService {

    @Autowired
    OmsTspeTimerImplementEvaCyclePlanMapper omsTspeTimerImplementEvaCyclePlanMapper;

    /**
     * 3-2-03批量改评价模板周期安排[2499]
     */
    @Trace(operationName = "3-2-03批量改评价模板周期安排")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchUpdateEvaTepCyclePlan(List<OmsTspeTimerImplementEvaCyclePlan> omsTspeTimerImplementEvaCyclePlan) {

        if (omsTspeTimerImplementEvaCyclePlan == null || omsTspeTimerImplementEvaCyclePlan.size() == 0) {
            return false;
        }
        return super.saveOrUpdateBatch(omsTspeTimerImplementEvaCyclePlan);

    }

    /**
     * 3-2-03查评价模板周期安排详情[2498]
     */
    @Trace(operationName = "3-2-03查评价模板周期安排详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public OmsTspeTimerImplementEvaCyclePlan queryEvaTepCyclePlanDetail(QueryEvaTepCyclePlanDetailReq reqDto) {

        try {

            Map<String, Object> columnMap = CommonFunctionHelper.objectToMap(reqDto);
            List<OmsTspeTimerImplementEvaCyclePlan> retList = (List<OmsTspeTimerImplementEvaCyclePlan>) super.listByMap(columnMap);
            if (retList == null || retList.size() <= 0) return null;
            OmsTspeTimerImplementEvaCyclePlan ret = retList.get(0);

            return ret;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }

    }

    /**
     * 3-2-03新增评价模板周期安排[2407]
     */
    @Trace(operationName = "3-2-03新增评价模板周期安排")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addEvaTepCyclePlan(OmsTspeTimerImplementEvaCyclePlan omsTspeTimerImplementEvaCyclePlan) {
        omsTspeTimerImplementEvaCyclePlan.setTimerEvaCyclePlanId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        omsTspeTimerImplementEvaCyclePlan.setSubjectLifeCycle("EDITING");
        omsTspeTimerImplementEvaCyclePlan.setIsArchive("FALSE");
        omsTspeTimerImplementEvaCyclePlan.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsTspeTimerImplementEvaCyclePlan.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        omsTspeTimerImplementEvaCyclePlan.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        omsTspeTimerImplementEvaCyclePlan.setIsValid("FALSE");
        if (super.save(omsTspeTimerImplementEvaCyclePlan)) {
            return omsTspeTimerImplementEvaCyclePlan.getTimerEvaCyclePlanId();
        } else {
            return "-1";
        }

    }

    /**
     * 3-2-03修改评价模板周期安排[2392]
     */
    @Trace(operationName = "3-2-03修改评价模板周期安排")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateEvaTepCyclePlan(OmsTspeTimerImplementEvaCyclePlan omsTspeTimerImplementEvaCyclePlan) {

        return super.updateById(omsTspeTimerImplementEvaCyclePlan);
    }

    /**
     * 3-2-03批量增评价模板周期安排
     */
    @Trace(operationName = "3-2-03批量增评价模板周期安排")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchAddEvaTepCyclePlan(List<OmsTspeTimerImplementEvaCyclePlan> omsTspeTimerImplementEvaCyclePlan) {

        if (omsTspeTimerImplementEvaCyclePlan == null || omsTspeTimerImplementEvaCyclePlan.size() == 0) {
            return false;
        }

        return super.saveBatch(omsTspeTimerImplementEvaCyclePlan.stream().map(item -> {
            item.setTimerEvaCyclePlanId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
            item.setSubjectLifeCycle("EDITING");
            item.setIsArchive("FALSE");
            item.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
            item.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
            item.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
            item.setIsValid("FALSE");
            item.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
            return item;
        }).collect(Collectors.toList()));
    }

    /**
     * 3-2-03查评价模板周期安排列表[2338]
     */
    @Trace(operationName = "3-2-03查评价模板周期安排列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsTspeTimerImplementEvaCyclePlan> queryEvaTepCyclePlanList(QueryEvaTepCyclePlanListReq reqDto) {

        try {
            QueryWrapper<OmsTspeTimerImplementEvaCyclePlan> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            reqDto.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
            reqDto.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID

            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsTspeTimerImplementEvaCyclePlan.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsTspeTimerImplementEvaCyclePlan.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsTspeTimerImplementEvaCyclePlan.class);
            CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<OmsTspeTimerImplementEvaCyclePlan>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }
}
